package com.ruyuan.commodity.common.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruyuan.commodity.model.goods.dos.SpecificationDO;
import com.ruyuan.commodity.model.goods.vo.SelectVO;
import com.ruyuan.commodity.model.goods.vo.SpecificationVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;

/**
 * SpecificationDO的Mapper
 */
@Repository
public interface SpecificationMapper extends BaseMapper<SpecificationDO> {

    /**
     * 查询分类关联的规格
     * @param categoryId 分类id
     * @return
     */
    @ResultType(SelectVO.class)
    @Select("select s.spec_id id , cs.category_id falgid , s.spec_name `text` " +
            "from es_specification s left join  es_category_spec cs on s.spec_id=cs.spec_id " +
            "and category_id=#{category_id} where s.seller_id=0 and s.disabled=1")
    List<SelectVO> getCatSpecification(@Param("category_id") Long categoryId);

    /**
     * 验证商家规格名称是否存在
     * @param map 条件
     * @return
     */
    @ResultType(Integer.class)
    @Select("select count(0) from es_specification s inner join es_category_spec cs on s.spec_id = cs.spec_id " +
            "where disabled = 1 and category_id = #{ew.category_id} and seller_id = #{ew.seller_id} and spec_name = #{ew.spec_name}")
    Integer checkSellerSpecName(@Param("ew") Map map);


    /**
     * 查询分类绑定的商家规格
     * @param categoryId 分类id
     * @param sellerId 商家id
     * @return
     */
    @ResultType(SpecificationVO.class)
    @Select("select s.spec_id,s.spec_name from es_specification s inner join es_category_spec cs on s.spec_id=cs.spec_id " +
            "where cs.category_id = #{category_id} and (s.seller_id=0 or s.seller_id=#{seller_id})")
    List<SpecificationVO> queryCatSpec(@Param("category_id") Long categoryId, @Param("seller_id")Long sellerId);

    /**
     * 查询分类绑定的平台规格
     * @param categoryId 分类id
     * @return
     */
    @ResultType(SpecificationVO.class)
    @Select("select s.spec_id,s.spec_name from es_specification s inner join es_category_spec cs on s.spec_id=cs.spec_id " +
            "where cs.category_id = #{category_id} and (s.seller_id=0)")
    List<SpecificationVO> queryAdminSpec(@Param("category_id") Long categoryId);

}
